<?php

class Account
{
    private $accountFile;
    private $accounts = [];

    public function __construct()
    {
        $this->accountFile = strtolower(__CLASS__) . '.json';
        if (file_exists($this->accountFile)) {
            $this->accounts = json_decode(file_get_contents($this->accountFile), true) ?: [];
        } else {
            touch($this->accountFile);
        }
    }

    public function storage()
    {
        file_put_contents($this->accountFile, json_encode($this->accounts));
    }

    public function store(array $params)
    {
        $this->accounts[$params['username']] = $params;
        $this->storage();
    }

    public function destroy($username)
    {
        unset($this->accounts[$username]);
        $this->storage();
    }

    public function all()
    {
        if (count($this->accounts)) {
            return $this->accounts;
        }
    }
}

class Bootstrap
{
    private $title;

    public function __construct($title = 'Authenticator')
    {
        $this->title = $title;
    }

    public function index()
    {
        $mfa = '<ol>';
        foreach ((new Account())->all() as $account) {
            $mfa .= '<li>';
            $mfa .= $account['domainName'];
            $mfa .= '(' . $account['username'] . ')';
            $mfa .= ' <a href="index.php?action=index">' . rand(111111, 99999) . '</a>';
            $mfa .= ' <a href="index.php?action=delete&username=' . $account['username'] . '">DELETE</a>';
            $mfa .= '</li>';
        }
        $mfa .= '</ol>';
        $this->page($mfa);
    }

    public function create()
    {
        $form = '<form action="index.php?action=store" method="POST">';
        $form .= '<p>UserName: <input type="text" name="UserName" /></p>';
        $form .= '<p>DomainName: <input type="text" name="DomainName" /></p>';
        $form .= '<p>SecretKey: <input type="text" name="SecretKey" /></p>';
        $form .= '<input type="submit" value="Submit" />';
        $form .= '</form>';
        $this->page($form);
    }

    public function delete($username)
    {
        (new Account())->destroy($username);
        header("Location: /index.php");
    }

    public function store()
    {
        $params['username'] = $_POST['UserName'];
        $params['domainName'] = $_POST['DomainName'];
        $params['secretKey'] = $_POST['SecretKey'];
        (new Account())->store($params);
        header("Location: /index.php");
    }

    private function page($mfa)
    {
        $this->container($this->header() . '<div="mfa">' . $mfa . '</div>' . $this->footer());
    }

    private function header()
    {
        return '<div class="header"><a href="index.php">Authenticator</a> <a href="index.php?action=create"> CREATE </a> </div>';
    }

    private function footer()
    {
        return '<div class="footer">&copy;' . date('Y') . ' ' . $this->title . '</div>';
    }

    private function container($container = '')
    {
        $this->html($this->title, '<div class="container">' . $container . '</div>');
    }

    private function html(string $title, string $body)
    {
        echo '<!doctype html>';
        echo '<html lang="zh">';
        echo '<head>';
        echo '<meta charset="utf-8">';
        echo '<title>' . $title . '</title>';
        echo '</head>';
        echo '<body>' . $body . '</body>';
        echo '</html>';
    }

}

$action = empty($_GET['action']) ? 'index' : $_GET['action'];

switch ($action) {
    case "index":
        (new Bootstrap())->index();
        break;

    case "create":
        (new Bootstrap())->create();
        break;

    case "delete":
        $username = empty($_GET['username']) ? $_GET['username'] : $_GET['username'];
        (new Bootstrap())->delete($username);
        break;

    case "store":
        (new Bootstrap())->store();
        break;
}
